<?php
class BeerDB extends SQLite3
{
    //
    //
    //
    function __construct( $path_adjust = "" )
    {
        $this->open($path_adjust . 'sqlite3_beer.db');
    }

    //
    //
    //
    function AddBeer($name, $description, $style, $abv, $ibu, $srm)
    {
        $result = true;

        if( $result )
        {
            if( !is_string( $name ) )
            {
                $result = false;
            }
            
            if( !is_string( $description ) )
            {
                $result = false;
            }
            
            if( !is_string( $style ) )
            {
                $result = false;
            }

            if( !is_float( $abv ) && !is_int( $abv ) )
            {
                $result = false;
            }
            
            if( !is_int( $ibu ) )
            {
                $result = false;
            }
            
            if( !is_float( $srm ) )
            {
                $result = false;
            }
            
        }

        if( $result )
        {
            $query = "
                INSERT INTO beer ( id, beername, description, style, abv, ibu, srm ) 
                VALUES ( NULL, :beername, :description, :style, :abv, :ibu, :srm)
                ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':beername', $name );
            $statement->bindValue( ':description', $description );
            $statement->bindValue( ':style', $style );
            $statement->bindValue( ':abv', $abv );
            $statement->bindValue( ':ibu', $ibu );
            $statement->bindValue( ':srm', $srm );

            $result |= ( $statement->execute() != false );

            $result |= $statement->close();
        }

        return $result;
    }

    //
    //
    //
    function DumpBeerTable()
    {
        $query = "
            SELECT * FROM beer WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }

    //
    //
    //
    function AddTap( $tapid, $beerid, $kegtypeid, $tapEpochDate )
    {
        $result = true;
        
        if( $result )
        {
            if( !is_int( $tapid ) )
            {
                $result = false;
            }
            
            if( !is_int( $beerid ) )
            {
                $result = false;
            }
            
            if( !is_int( $tapEpochDate ) )
            {
                $result = false;
            }
            
            if( !is_int( $kegtypeid ) )
            {
                $result = false;
            }
        }
        
        if( $result )
        {
            $query = "
                INSERT INTO tap ( id, beer_id, kegtype_id, tapepochdate  ) VALUES ( :id, :beerid, :kegtypeid, :tapepochdate )
                ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':id', $tapid );
            $statement->bindValue( ':beerid', $beerid );
            $statement->bindValue( ':kegtypeid', $kegtypeid );
            $statement->bindValue( ':tapepochdate', $tapEpochDate );

            $result |= ( $statement->execute() != false );
            $result |=  $statement->close();
        }
        
        return $result;
    }
    
    //
    //
    //
    function DumpTapTable()
    {
        $query = "
            SELECT * FROM tap WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }
    
    //
    //
    //
    function DumpFlowmeterTable()
    {
        $query = "
            SELECT * FROM flowmeter WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }
    
    //
    //
    //
    function AddTemperature( $temperature, $temperatureepochdate )
    {
        $result = true;
        
        if( $result )
        {
            if( !is_real( $temperature ) )
            {
                $result = false;
            }
            
            if( !is_int( $temperatureepochdate ) )
            {
                $result = false;
            }
        }
        
        if( $result )
        {
            $query = "
                INSERT INTO temperature ( temperature, temperatureepochdate ) 
                    VALUES ( :temperature, :temperatureepochdate )
                ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':temperature', $temperature );
            $statement->bindValue( ':temperatureepochdate', $temperatureepochdate );

            $result |= ( $statement->execute() != false );
            $result |=  $statement->close();
        }
        
        return $result;
    }
    
    //
    //
    //
    function DumpTemperatureTable()
    {
        $query = "
            SELECT * FROM temperature WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }
    
    //
    //
    //
    function AddKeg( $name, $capacityinoz )
    {
        $result = true;
        
        if( $result )
        {
            if( !is_string( $name ) )
            {
                $result = false;
            }
            
            if( !is_int( $capacityinoz ) )
            {
                $result = false;
            }
        }
        
        if( $result )
        {
            $query = "
                INSERT INTO kegtype ( id, capacityinoz, kegname ) 
                    VALUES ( NULL, :capacityinoz, :kegname )
                ";
                
            $statement = $this->prepare( $query );
            $statement->bindValue( ':capacityinoz', $capacityinoz );
            $statement->bindValue( ':kegname', $name );

            $result |= ( $statement->execute() != false );
            $result |=  $statement->close();
        }
        
        return $result;
    }
    
    //
    //
    //
    function DumpKegtypeTable()
    {
        $query = "
            SELECT * FROM kegtype WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }
    
    //
    //
    //
    function GetMostRecentTemperatureReadings( $howMany )
    {
        $result = true;
        
        if( $result )
        {
            if( !is_int( $howMany ) )
            {
                $result = false;
            }
        } 
        
        if( $result )
        {
            $query = "
                SELECT temperature, temperatureepochdate
                FROM
                (
                     SELECT *
                     FROM temperature
                     ORDER BY temperatureepochdate DESC
                     LIMIT $howMany
                ) T1
                ORDER BY temperatureepochdate
                ";

            $statement = $this->prepare( $query );

            $result = $statement->execute();
        }
        
        return $result;
    }
    
    //
    //
    //
    function GetTemperatureReadingsSince( $epochtime )
    {
        $result = true;
        
        if( $result )
        {
            if( !is_int( $epochtime ) )
            {
                $result = false;
            }
        } 
        
        if( $result )
        {
            $query = "
                SELECT *
                FROM temperature
                WHERE temperatureepochdate >= :sincedate;
                ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':sincedate', $epochtime );

            $result = $statement->execute();
        }
        
        return $result;
    }


    //
    //
    //
    function GetNumInstalledTaps()
    {
        $result = true;
      
        if( $result )
        {
            $query = "
                SELECT COALESCE (MAX (id ) + 1, 0 ) FROM tap;
                ";

            $statement = $this->prepare( $query );

            $sqlresult = $statement->execute();
            
            while( $row = $sqlresult->fetchArray( SQLITE3_NUM) )
            {
                $result = $row[0];
            }
        }
        
        return $result;
    }
    
    //
    //
    //
    function GetTapBeerData( $tapId )
    {
        $result = true;
      
        if( $result )
        {
            $query = "
                SELECT *
                FROM tap, beer, kegtype
                WHERE tap.id == :tapid
                AND kegtype.id == tap.kegtype_id
                AND tap.beer_id == beer.id
                AND tap.tapepochdate == ( SELECT MAX( tapepochdate ) FROM tap WHERE tap.id == :tapid );
            ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':tapid', $tapId );

            $result = $statement->execute();
        }
        
        return $result;
    }
    
    //
    //
    //
    function GetTapPourData( $tapId )
    {
        $result = true;
      
        if( $result )
        {
            $query = "
                SELECT *
                FROM tap, beer, pour
                WHERE tap.id == :tapid
                AND tap.beer_id == beer.id
                AND tap.tapepochdate <= pour.pourepochdate;
            ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':tapid', $tapId );

            $result = $statement->execute();
        }
        
        return $result;
    }
    
    //
    //
    //
    function SetTapPercentRemaining( $tapId, $percentRemaining )
    {
        $result = true;
        
        if( $result )
        {
            $query = "
                DELETE FROM remaining WHERE tap_id == :tapid;
                ";
                
            $statement = $this->prepare( $query );
            $statement->bindValue( ':tapid', $tapId );
            
            $result = $statement->execute();
        }
        
        if( $result )
        {
            $query = "
                INSERT INTO remaining ( tap_id, percentremaining )
                    VALUES( :tapid, :percentRemaining );
                ";
                
            $statement = $this->prepare( $query );
            $statement->bindValue( ':tapid', $tapId );
            $statement->bindValue( ':percentRemaining', $percentRemaining );
                
            $result = $statement->execute();
        }
        
        return $result;
    }
    
    //
    //
    //
    function GetRemainingPercentage( $tapId )
    {
    
        $result = true;
        
        if( $result )
        {
            $query = "
                SELECT * FROM remaining 
                    WHERE tap_id = :tapid;
                ";

            $statement = $this->prepare( $query );
            $statement->bindValue( ':tapid', $tapId );

            $result = $statement->execute();
        }

        return $result;
    }
    
    //
    //
    //
    function DumpRemainingTable()
    {
        $query = "
            SELECT * FROM remaining WHERE 1;
            ";

        $statement = $this->prepare( $query );

        $result = $statement->execute();

        return $result;
    }
    
    
}
?>


